<?php 
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/
class notesComponents extends sfComponents {
	
	public function executeRelated() {
		$this->notes_pager = NotePeer::retrieveByCourseIdPager($this->course_id, $this->getUser()->getId(), 1, 4);
	}
	
	public function executeRelatedGroups() {
		$this->groups_pager = GroupCoursePeer::retrieveByCourseIdPager($this->course_id, 1, 3);
	}
	
	public function executePostNotes(){
		$this->group_courses = GroupCoursePeer::retrieveByGroupId($this->group_id);
		
	}
	
	public function executeFilter(){
		$this->semesters = SemesterPeer::retrieveAll();
	}
	
	public function executeProfileList(){
		
		//if current user matches user id for list then surpass security
		if($this->getUser()->getId() == $this->user_id)
		{
		
			$this->notes = NotePeer::retrieveByUserId($this->user_id);
		}
		else
		{
			$notes = NotePeer::retrieveByUserId($this->user_id);
			$result = array();
			foreach($notes as $note)
			{
				if(EntitlementPeer::isEntitled($note->get_id, $this->getUser()->getid(), EntitlementPeer::NOTE_TYPE, EntitlementPeer::READ_PERMISSION))
				{
				$result[]=$note;
				}
			}
			$this->notes = $result;
		}
		
		
	}
	
	public function executeProfileDetail(){
		
		//if current user matches user id for list then surpass security
		if($this->getUser()->getId() == $this->user_id)
		{
		
			$this->notes = NotePeer::retrieveByUserId($this->user_id);
		}
		else
		{
			$notes = NotePeer::retrieveByUserId($this->user_id);
			$result = array();
			foreach($notes as $note)
			{
				if(EntitlementPeer::isEntitled($note->getId(), $this->getUser()->getId(), EntitlementPeer::NOTE_TYPE, EntitlementPeer::READ_PERMISSION))
				{
				$result[]=$note;
				}
			}
			$this->notes = $result;
		}
		
	}
	
	public function executeProfileCourse(){
		
		
		//if current user matches user id for list then surpass security
		if($this->getUser()->getId() == $this->user_id)
		{
		
			$this->notes = NotePeer::retrieveByUserId($this->user_id);
		}
		else
		{
			$notes = NotePeer::retrieveByUserId($this->user_id);
			$result = array();
			foreach($notes as $note)
			{
				if(EntitlementPeer::isEntitled($note->getId(), $this->getUser()->getId(), EntitlementPeer::NOTE_TYPE, EntitlementPeer::READ_PERMISSION))
				{
					$result[]=$note;
				}
			}
			$this->notes = $result;
		}
		
		//segment the answer by semester
		$course_array = array();
		
		foreach($this->notes as $note)
		{
	
			//put each unique semester into array
			$course = $note->getCourseId();//CourseSemesterPeer::retrieveByCourseId($course->getId());
			
			$course = CoursePeer::retrieveById($course);
			$course_name = $course->getName();
				
			if(isset($course_array[$course_name]))
			{
				$course_array[$course_name]['notes'][] = $note; 
			
			}
			else
			{
				$course_array[$course_name] = array();
				$course_array[$course_name]['obj'] = $course;
				$course_array[$course_name]['notes'] = array(); 
				$course_array[$course_name]['notes'][] = $note; 
			}
		}
		
		$this->courses = $course_array;
		
		
		
	
	}

}